<?php

use App\Helpers\ScopeHelper;
use Illuminate\Http\Request;

use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;

/*
|--------------------------------------------------------------------------
| ADMIN Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

/**
 * 路由
 * http://backend.manager.my/api/admin/users
 */

Route::post('/login', 'Admin\LoginController@login')->name('login.login');

Route::post('/token/refresh', 'Admin\LoginController@login')->name('login.refresh');

Route::middleware('auth:admin','scopes:admin_base')->group(function() {
    Route::post('/logout', 'Admin\LoginController@logout')->name('login.logout');

    /**
     * 作用域：超管 运营总监 运营专员 财务 客服经理 客服专员 产品经理
     */
    Route::middleware('auth:admin','scope:'.ScopeHelper::getAdminScope('super'))->group(function() {
        /**
         * 首页
         */
        Route::get('/index', function (Request $request) {
            return response()->json([
                'status' => 'success',
                'status_code' => 200,
                'data' => $request->user()
            ],200);
        });
    });

    /**
     * 作用域：超管 运营总监 运营专员 客服经理 客服专员
     */
    Route::middleware('auth:admin','scope:'.ScopeHelper::getAdminScope('level6'))->group(function() {
        /**
         * 用户
         */
        Route::get('/user/index', function (Request $request) {
            $user = $request->user(); // Returns an instance of `Admin` or `User`
            return response()->json([
                'status' => 'success',
                'status_code' => 200,
                'data' => [
                    'name' => $user['name'],
                    'email' => $user['email']
                ]
            ],200);
        });

        /**
         * 经销商
         */
        Route::get('/agency/index', function (Request $request) {
            $user = $request->user(); // Returns an instance of `Admin` or `User`
            return response()->json([
                'status' => 'success',
                'status_code' => 200,
                'data' => [
                    'name' => $user['name'],
                    'email' => $user['email']
                ]
            ],200);
        });

        /**
         * 店铺
         */
        Route::get('/shop/index', function (Request $request) {
            $user = $request->user(); // Returns an instance of `Admin` or `User`
            return response()->json([
                'status' => 'success',
                'status_code' => 200,
                'data' => [
                    'name' => $user['name'],
                    'email' => $user['email']
                ]
            ],200);
        });

        /**
         * 订单
         */
        Route::get('/order/index', function (Request $request) {
            $user = $request->user(); // Returns an instance of `Admin` or `User`
            return response()->json([
                'status' => 'success',
                'status_code' => 200,
                'data' => [
                    'name' => $user['name'],
                    'email' => $user['email']
                ]
            ],200);
        });
    });

    /**
     * 作用域：超管 运营总监 运营专员
     */
    Route::middleware('auth:admin','scope:'.ScopeHelper::getAdminScope('level4'))->group(function() {
        /**
         * 营销
         */
        Route::get('/marking/index', function (Request $request) {
            return response()->json([
                'status' => 'success',
                'status_code' => 200,
                'data' => $request->user()
            ],200);
        });

        /**
         * 广告
         */
        Route::get('/advertising/index', function (Request $request) {
            return response()->json([
                'status' => 'success',
                'status_code' => 200,
                'data' => $request->user()
            ],200);
        });

        /**
         * 优惠券
         */
        Route::get('/coupon/index', function (Request $request) {
            return response()->json([
                'status' => 'success',
                'status_code' => 200,
                'data' => $request->user()
            ],200);
        });

        /**
         * 魔力
         */
        Route::get('/magic/index', function (Request $request) {
            return response()->json([
                'status' => 'success',
                'status_code' => 200,
                'data' => $request->user()
            ],200);
        });

        /**
         * 插件
         */
        Route::get('/plugin/index', function (Request $request) {
            return response()->json([
                'status' => 'success',
                'status_code' => 200,
                'data' => $request->user()
            ],200);
        });

        /**
         * 消息
         */
        Route::get('/message/index', function (Request $request) {
            return response()->json([
                'status' => 'success',
                'status_code' => 200,
                'data' => $request->user()
            ],200);
        });
    });

    /**
     * 作用域：超管 财务
     */
    Route::middleware('auth:admin','scope:'.ScopeHelper::getAdminScope('level2'))->group(function() {
        /**
         * 财务
         */
        Route::get('/fiance/index', function (Request $request) {
            return response()->json([
                'status' => 'success',
                'status_code' => 200,
                'data' => $request->user()
            ],200);
        });
    });

    /**
     * 作用域：超管 运营总监 运营专员 产品经理
     */
    Route::middleware('auth:admin','scope:'.ScopeHelper::getAdminScope('level5'))->group(function() {
        /**
         * 统计
         */
        Route::get('/statistics/index', function (Request $request) {
            return response()->json([
                'status' => 'success',
                'status_code' => 200,
                'data' => $request->user()
            ],200);
        });
    });

    /**
     * 作用域：超管
     */
    Route::middleware('auth:admin','role:super_admin','permission:del_agency|add_agency','scope:'.ScopeHelper::getAdminScope('level1'))->group(function() {
        /**
         * 系统->管理员
         */
        Route::get('/system/admin', function (Request $request) {
            return response()->json([
                'status' => 'success',
                'status_code' => 200,
                'data' => $request->user()
            ],200);
        });

        /**
         * 权限管理
         */
        Route::post('/role/index', function (Request $request) {

            //获取所有已定义的角色的集合
            $roles = $request->user()->getRoleNames();

            // 返回角色是 'writer' 的用户
            //$users = User::role('writer')->get();

            return response()->json([
                'status' => 'success',
                'status_code' => 200,
                'data' => $roles
            ],200);
        });

        Route::post('/role/add', function (Request $request) {

            $role = Role::create(['name' => $request->request->get('role_name')]);

            return response()->json([
                'status' => 'success',
                'status_code' => 200,
                'data' => $role
            ],200);
        });

        Route::post('/role/assort', function (Request $request) {

            //分配角色给任何一个用户
            $permission = $request->user()->assignRole(['super_admin']);

            return response()->json([
                'status' => 'success',
                'status_code' => 200,
                'data' => $permission
            ],200);
        });

        Route::post('/permission/index', function (Request $request) {

            //获取直接分配给用户的所有的权限
            $permissions = $request->user()->permissions;
            //返回所有用户通过赋予角色所继承的权限
            //$permissions = $request->user()->getAllPermissions();
            // 只返回有 'edit articles' 权限的用户 （继承角色得来的或者是直接分配的）
            //$users = User::permission('edit articles')->get();

            return response()->json([
                'status' => 'success',
                'status_code' => 200,
                'data' => $permissions
            ],200);
        });

        Route::post('/permission/add', function (Request $request) {

            $permission = Permission::create(['name' => $request->request->get('permission_name')]);

            return response()->json([
                'status' => 'success',
                'status_code' => 200,
                'data' => $permission
            ],200);
        });

        Route::post('/permission/assort', function (Request $request) {

            //分配权限给任何一个用户
            $permission = $request->user()->givePermissionTo(['add_agency']);

            return response()->json([
                'status' => 'success',
                'status_code' => 200,
                'data' => $permission
            ],200);
        });

        Route::post('/permission/role/assort', function (Request $request) {

            //权限分配给一个角色
//            $role = Role::findByName('super_admin');
//            $permission = $role->givePermissionTo('add_agency');

            //直接分配给用户的直接权限
            $permission = $request->user()->givePermissionTo('edit_agency');

            return response()->json([
                'status' => 'success',
                'status_code' => 200,
                'data' => $permission
            ],200);
        });
    });

    /**
     * 作用域：超管 运营总监
     */
    Route::middleware('auth:admin','scope:'.ScopeHelper::getAdminScope('level3'))->group(function() {
        /**
         * 系统->地区
         */
        Route::get('/system/area', function (Request $request) {
            return response()->json([
                'status' => 'success',
                'status_code' => 200,
                'data' => $request->user()
            ],200);
        });

        /**
         * 系统->参数
         */
        Route::get('/system/parameter', function (Request $request) {
            return response()->json([
                'status' => 'success',
                'status_code' => 200,
                'data' => $request->user()
            ],200);
        });
    });

});